package co.edu.eam.ingsoft.jpa.reflect;

import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

public class EntityProcessor {

	public String getTableName(Class<?> clazz) {
		// Class<?> clazz = entity.getClass();

		String tableName = "";

		if (clazz.isAnnotationPresent(Entity.class)) {
			if (clazz.isAnnotationPresent(Table.class)) {
				Table table = clazz.getAnnotation(Table.class);
				tableName = table.name();
			}
			if (tableName.isEmpty()) {
				tableName = clazz.getSimpleName();
			}
		}
		return tableName;
	}

	public String getFieldId(Class<?> clazz) {
		// Class<?> clazz = entity.getClass();

		String fieldId = "";

		Method[] methods = clazz.getDeclaredMethods();
		for (Method method : methods) {
			if (method.isAnnotationPresent(Id.class)) {
				String returnType = "";
				if (method.getReturnType() == int.class
						|| method.getReturnType() == Integer.class
						|| method.getReturnType() == long.class
						|| method.getReturnType() == Long.class
						|| method.getReturnType() == double.class
						|| method.getReturnType() == Double.class) {
					returnType = " NUMBER";
				} else if (method.getReturnType() == String.class) {
					returnType = " VARCHAR2(255)";
				} else if (method.getReturnType() == Date.class) {
					returnType = " DATE";
				}

				fieldId = method.getName().substring(3, 4).toLowerCase()
						+ method.getName().substring(4) + returnType;
				break;
			}
		}
		// if (fieldId.isEmpty()) {
		// Field[] fields = clazz.getDeclaredFields();
		// for (Field field : fields) {
		// if (field.isAnnotationPresent(Id.class)) {
		// fieldId = field.getName();
		// break;
		// }
		// }
		// }
		return fieldId;
	}

	public String getIdValue(Object entity) {
		Class<?> clazz = entity.getClass();

		String idValue = "";

		Method[] methods = clazz.getDeclaredMethods();
		for (Method method : methods) {
			if (method.isAnnotationPresent(Id.class)) {
				try {
					idValue = method.invoke(entity).toString();
				} catch (Exception e) {
					e.printStackTrace();
				}
				break;
			}
		}
		// if (idValue.isEmpty()) {
		// Field[] fields = clazz.getDeclaredFields();
		// for (Field field : fields) {
		// if (field.isAnnotationPresent(Id.class)) {
		// idValue = field.getName();
		// break;
		// }
		// }
		// try {
		// Method method = clazz.getMethod("get"
		// + idValue.substring(0, 1).toLowerCase()
		// + idValue.substring(1));
		// idValue = method.invoke(entity).toString();
		// } catch (Exception e) {
		// e.printStackTrace();
		// }
		// }
		return idValue;
	}

	public ArrayList<String> getColumnsName(Class<?> clazz) {
		// Class<?> clazz = entity.getClass();

		ArrayList<String> columns = new ArrayList<String>();
		String columnTemp = "";
		Method[] methods = clazz.getDeclaredMethods();
		for (Method method : methods) {
			if (method.isAnnotationPresent(Column.class)) {
				String returnType = "";
				if (method.getReturnType() == int.class
						|| method.getReturnType() == Integer.class
						|| method.getReturnType() == long.class
						|| method.getReturnType() == Long.class
						|| method.getReturnType() == double.class
						|| method.getReturnType() == Double.class) {
					returnType = " NUMBER";
				} else if (method.getReturnType() == String.class) {
					returnType = " VARCHAR2(255)";
				} else if (method.getReturnType() == Date.class) {
					returnType = " DATE";
				}

				Column column = method.getAnnotation(Column.class);
				columnTemp = column.name();
				if (column.name().isEmpty()) {
					columnTemp = method.getName().substring(3, 4).toLowerCase()
							+ method.getName().substring(4);
				}
				columns.add(columnTemp + returnType);
			}
		}
		// if (columns.isEmpty()) {
		// Field[] fields = clazz.getDeclaredFields();
		// for (Field field : fields) {
		// if (field.isAnnotationPresent(Column.class)) {
		// Column column = field.getAnnotation(Column.class);
		// columnTemp = column.name();
		// if (column.name().isEmpty()) {
		// columnTemp = field.getName();
		// }
		// columns.add(columnTemp);
		// }
		// }
		// }
		return columns;
	}

	public ArrayList<String> getColumnsValues(Object entity) {
		Class<?> clazz = entity.getClass();

		ArrayList<String> values = new ArrayList<String>();
		String valueTemp = "";
		Method[] methods = clazz.getDeclaredMethods();
		for (Method method : methods) {
			if (method.isAnnotationPresent(Column.class)) {
				try {
					if (method.getReturnType() == Date.class) {
						Date fecha = (Date) method.invoke(entity);
						SimpleDateFormat format = new SimpleDateFormat(
								"dd/MM/yyyy");
						valueTemp = format.format(fecha);
					} else {
						valueTemp = method.invoke(entity).toString();
					}
					values.add(valueTemp);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return values;
	}
}
